using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Dracham.Framework;
using Dracham.Components.GameScreens;

namespace Dracham
{
    public class Dracham : Game
    {
        #region Fields

        protected static readonly Dracham instance = new Dracham();

        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        private Stack<IGameScreen> gameScreens = new Stack<IGameScreen>();

        #endregion

        #region Constructors

        protected Dracham()
        {
            this.graphics = new GraphicsDeviceManager(this);
            this.Content.RootDirectory = "Content";

            this.IsMouseVisible = true;

            this.graphics.PreferredBackBufferWidth = 1024;
            this.graphics.PreferredBackBufferHeight = 768;

            this.graphics.ApplyChanges();
        }

        #endregion

        #region Properties

        public static Dracham Instance
        {
            get { return Dracham.instance; }
        }

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
        }

        public IGameScreen CurrentGameScreen
        {
            get
            {
                return this.gameScreens.Count == 0 ? null : this.gameScreens.Peek();
            }
        }

        #endregion

        #region Methods

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            this.spriteBatch = new SpriteBatch(GraphicsDevice);

            //
            // Add GameScreens
            //

            
            this.gameScreens.Push(new TestScreen());
            this.gameScreens.Push(new DemoMapScreen());
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (this.gameScreens.Count == 0)
            {
                this.Exit();
                return;
            }

            //
            // Insert various update-logic here
            // Input, AI, etc.
            //

            if (this.gameScreens.Peek().Update(gameTime)) this.gameScreens.Pop();
        }

        protected override void Draw(GameTime gameTime)
        {
            this.GraphicsDevice.Clear(Color.Black);

            if (this.gameScreens.Count == 0) return;

            this.gameScreens.Peek().Draw(gameTime);
        }

        #endregion
    }
}
